package com.stone.business.sbqxjl.action;

/**
 * <pre>
 * 设备缺陷记录
 * @author 
 * </pre>
 */

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;

import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class SbqxjlAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private ZbjlCommonService myService;
	
	private TSbqxjl entity;
	
	private String id;
	private String qxly;
	private String bdz;
	private String bdzname;
	// 设备类型
	private List<TDictionary> sblxList;
	
	// 缺陷类别
	private List<TDictionary> qxlbList;
	// 缺陷来源
	private List<TDictionary> qxlyList;
	

	// 调度员
	private List<TDictionary> ddyList;
	
	// 到位人员
	private List<TDictionary> dwryList;
	private String state;
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		
		// 修改进入
		if (!"0".equals(id)) {
			
			this.entity = (TSbqxjl)myService.findById(Long.parseLong(id), TSbqxjl.class.getName());

			// 新建进入
		} else {
			
			this.entity = new TSbqxjl();
			this.entity.setState(super.getSearchBean().getCon1());
			this.entity.setId(Long.parseLong("0"));
			this.entity.setQxly(qxly);
			this.entity.setBdz(bdz);
			 bdzname = (String) super.getRequest().getParameter("bdzname");
			 /*try {
				bdzname= new String(bdzname.getBytes("GBK"), "iso8859-1");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			//bdzname=bdzname.substring(0,bdzname.length()-1);
			this.entity.setBdzname(bdzname);
			// 发现日期
			String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
			this.entity.setFxrq(DateFormatTool.formatToDate(sysdate, true));
			
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			
//			// 缺陷编号
//			List<HashMap<String,Object>> list = myService.findListBySql("SELECT MAX(QXBH) as QXBH FROM TAB_SBQXJL where QXBH like '" + sysdate.replaceAll("-", "").substring(0,8) + "%'");
//			
//			HashMap<String,Object> map = list.get(0);
//			
//			if (map.get("qxbh") != null && !StringUtils.isNullOrBlank(map.get("qxbh").toString())) {
//				
//				this.entity.setQxbh(String.valueOf(Long.parseLong((String) map.get("qxbh")) + 1));
//			} else {
//				this.entity.setQxbh(sysdate.replaceAll("-", "").substring(0,8) + "00001");
//			}

		}
		
		// 获得字典
		getDic();
		
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		// 系统时间
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");

		// 新建时
		if (entity.getId() == 0) {
			
//			// 缺陷编号
//			List<HashMap<String,Object>> list = myService.findListBySql("SELECT MAX(QXBH) as QXBH FROM TAB_SBQXJL where QXBH like '" + sysdate.replaceAll("-", "").substring(0,8) + "%'");
//			
//			HashMap<String,Object> map = list.get(0);
//			
//			if ((String) map.get("qxbh") != null && !StringUtils.isNullOrBlank(map.get("qxbh").toString())) {
//				
//				this.entity.setQxbh(String.valueOf(Long.parseLong((String) map.get("qxbh")) + 1));
//			} else {
//				this.entity.setQxbh(sysdate.replaceAll("-", "").substring(0,8) + "00001");
//			}

			//entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
			entity.setOuid(organCode);
			entity.setCjsj(DateFormatTool.formatToDate(sysdate,true));
			myService.save(entity);	
			
		} else {
//			by wangtao add20110330
			if(entity.getXcsj()!=null&&!StringUtils.isNullOrBlankFull(entity.getXcrname())&&!StringUtils.isNullOrBlankFull(entity.getXcyy())&&StringUtils.isNullOrBlankFull(entity.getCjr1())){
				entity.setCjr1(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
				entity.setCjrid1(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
				entity.setCjsj1(DateFormatTool.formatToDate(sysdate,true));
				
			}
	
			entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setXgrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
			myService.update(entity);
		}
		
		// 获得字典
		getDic();

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	
	/**
	 * 查询字典项目
	 */
	public void getDic() {
		
		// 设备类型
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		sblxList = dicService.findList("SBLX");
		
		// 缺陷类别
		qxlbList = dicService.findList("QXLB");
		// 缺陷来源
		//qxlyList = dicService.findList("QXLY");
		qxlyList = dicService.findListByHql(" from TDictionary where itemType = 'QXLY' and itemId = '"+this.entity.getQxly()+"'");
			if(super.getSession().getAttribute(IConstant.SESSION_ISADMIN).equals("1")){
				qxlyList = dicService.findList("QXLY");
	         }
		
		// 调度员
		//ddyList = dicService.findListByHql(" from TBzUser where userstate != '1' and usertype = (select id from TDictionary where itemType = 'BZ' and itemId = '01')");
		
		// 到位人员
		//dwryList = dicService.findListByHql(" from TBzUser where usertype = (select id from TDictionary where itemType = 'BZ' and itemId = '12')");
		dwryList = myService.getListByDHql("u.id as userid, u.name as username", "u.id = ur.userId and ur.roleId = r.id and r.nameen in('ZBZ','BZZ','GLRY')", null, "group by u.id ,u.name order by u.id", "TUser u, TUserRole ur , TRole r");
	}
	/**
	 * by wangtao add20110115
	 */
	public void sbqxFileCountCheck() throws Exception {
		
		long count = myService.getTotalByDHql("tableid = ? and colname = 'TAB_SBQXJL-QXZP'", new Object[]{Long.parseLong(this.id)}, "TFile");
		
		if (count == 0) {
			super.getResponse().getWriter().print("{count:0}");
		} else {
			super.getResponse().getWriter().print("{count:1}");
		}
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		myService.deleteSbqxjl(id);
		
		return "DEL-SUCCESS";
		
	}
	/**
	 * 手动归档
	 * @return
	 * @throws AppException
	 */
	public String history() throws AppException {
		
//		 状态修改
		entity.setState(state);
		myService.update(entity);
		// 获得字典
		getDic();
		
		super.setInfo("归档成功");
		
		return SUCCESS;
	}
	/**
	 * 驳回
	 * @return
	 * @throws AppException
	 */
	public String reject() throws AppException {
		
//		 状态修改
		entity.setState(state);
		myService.update(entity);
		// 获得字典
		getDic();
		
		super.setInfo("驳回成功");
		
		return SUCCESS;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.myService = myService;
	}

	public TSbqxjl getEntity() {
		return entity;
	}

	public void setEntity(TSbqxjl entity) {
		this.entity = entity;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public List<TDictionary> getQxlbList() {
		return qxlbList;
	}

	public List<TDictionary> getDdyList() {
		return ddyList;
	}

	public List<TDictionary> getDwryList() {
		return dwryList;
	}
	
	public List<TDictionary> getQxlyList() {
		return qxlyList;
	}

	public String getQxly() {
		return qxly;
	}

	public void setQxly(String qxly) {
		this.qxly = qxly;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

}
